$(document).ready(function(){
    const baseUrl = "https://mock.mengxuegu.com/mock/615468f4697a201039f53bee/gzy";

    // 轮播图数据渲染
    let bannerRes = request("/json/iaa/banner");
    console.log(bannerRes);
    request("/json/iaa/banner").then(res => {
        let {banner} = res.data.data;
        console.log(banner);
        banner.forEach((item,index) => {
            if(index == 0){
                $(".thumnail").append(`<img src="${item.url}" alt="${item.alt}" title="${item.description}" class="thum-active">`);
            }else{
                $(".thumnail").append(`<img src="${item.url}" alt="${item.alt}" title="${item.description}">`);
            }
            $(".banner-img").append(`<a href="#"><img src="${item.url}" alt="${item.alt}" title="${item.description}"></a>`)
           
        })
        
    
    })



    // 导航栏切换
    $(".btn-hide").click(function(){
        $(".nav-wrap").slideToggle(300);
    });

    // 导航栏
    let index = 0;
    let timer;
    let ele_length = $(".banner-img img").length; 
    auto()

    function auto (){
        show();
        showtime();
    }


    function show(){
        $(".banner-img a").eq(index).show().siblings("a").hide();
        $(".thumnail img").eq(index).addClass("thum-active").siblings("img").removeClass("thum-active");
    }

    function showtime(){
        timer = setInterval(()=>{
            index++;
            if(index == 3){
                index = 0
            }
            show();
            
            
        },3000)
    }

    $(".thumnail").on("click","img",function(){
        clearInterval(timer);
        index = $(this).index();
        auto();
    })


    $(".banner-btn div").eq(0).on("click",function(){
        clearInterval(timer);
        index--;
        if(index == -1){
            index = 2;
        }
        auto();

    })

    $(".banner-btn div").eq(1).on("click",function(){
        clearInterval(timer);
        index++;
        if(index == 3){
            index = 0;
        }
        auto();

    });


    
    // 倒计时
    // let countdown_timer;
    countdonwFn();
    
    async function countdonwFn(){
        let res =  await request("/json/iaa/countdown");
        let {target} = res.data.countdown;
        let targetStamp =  Date.parse(new Date(target));

        setInterval(()=> {
            let {d,h,m,s} = countdonw(targetStamp);
            // console.log(d,h,m,s);
            $(".d").text(d);
            $(".h").text(h);
            $(".m").text(m);
            $(".s").text(s);
        },1000)
       
    }

    function countdonw(targetStamp){
        let nowdate = Date.parse(new Date());
        let leftStamp = targetStamp-nowdate;
        let d,h,m,s;
            d = Math.floor(leftStamp/1000/60/60/24%24);
            d = d<10 ?"0"+d:d;
            h = Math.floor(leftStamp/1000/60/60%24);
            h = h<10 ?"0"+h:h;
            m = Math.floor(leftStamp/1000/60%60);
            m = m<10 ?"0"+m:m;
            s = Math.floor(leftStamp/1000%60);
            s = s<10 ?"0"+s:s;
        return {d,h,m,s}

    }
    

    //最近新闻
    request("/json/iaa/news").then(res => {
        let newsData = res.data.data
        let str = "";
        newsData.forEach(item => {
            str += `
            <div class="news-item">
                <div class="news-img">
                    <img src="${item.image}" alt="${item.category}" title="${item.category}" >
                </div>
                <h3>${item.title}</h3>
                <p>${item.description}</p>
                <p>${item.data}</p>
            </div>
            
            `
        });

        $(".news-wrap").html(str);
        tempImg($(".news-wrap img"));
        // 图片加载失败默认显示展位图
        function tempImg(element){
            $(element).on("error",function(){
                $(this).attr("src","./img/error.jpg")
            })
        }

        // console.log(str);


    })


    // 赞助商
    $(".sponsors-wrap ul li ").on("click",function(){
        let index_sponsors = $(this).index()
        $(this).addClass("sponsors-active").siblings("li").removeClass("sponsors-active");
        $(".sponsors-wrap .spronsors-control").eq(index_sponsors).show().siblings("div.spronsors-control").hide()

    })


    // 表单验证
    let vail_flag = false;
    let regNum = /[1,5]/;

    $(".submit-btn").on("click" ,function(){
        
        $(".form-control input").each((index,item) => {
            vail_flag = vail_nll(item);
        })

        if($(".tic-time select").val() == ""){
            vail_flag = false
            $(".tic-time select").siblings(".vail-from").html("值不能为空");
        }else{
            $(".tic-time select").siblings(".vail-from").html("");
        }

       
        if(!regNum.test($(".tic-atten input").val())){
            $(".tic-atten input").siblings(".vail-from").html("请输入1-5的值");
        }else{
            $(".tic-atten input").siblings(".vail-from").html("");
        }
      





        return vail_flag;
    })

    // 表单判空
    function vail_nll(item){
        if($(item).val().trim().length == 0){
            $(item).siblings(".vail-from").html("值不能为空");
            return false
        }else{
            $(item).siblings(".vail-from").html("");
            return true
        }
    }

    // 动画
    let topLeft = 0 ;
    let animaTimer = setInterval(() => {
        
        let ulWidth =  $(".media-wrap ul").innerHeight();
        
        topLeft -= 120;
        // console.log(topLeft,ulWidth);
        if(ulWidth <= Math.abs(topLeft) ){
            topLeft = 0;
        }

        $(".media-wrap ul").animate({
            top:topLeft+"px"
        },"slow");

    },3000)


    // 打开页面加载动画
    let loadTimer = setTimeout(()=>{
        $(".animate").remove();
    },3000)

    // 请求函数
   async function request(url){
       return axios({url:baseUrl+url})
   }




})